package com.augurit.gis.common.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.sql.DataSource;


/**
 * <p>Title: JDBC的数据源工厂类</p>
 * <p>Description: 对系统的各种数据源进行集中配置,提供创建数据连接的接口</p>
 * <p>Company: 广州奥格智能科技有限公司</p>
 * @author 沈坚丰
 * @date 2009-05-04
 */

public class ConnectionFactory {
	private static Locale locale = Locale.getDefault(); 
	private static ResourceBundle localResource = ResourceBundle.getBundle("gis/missource", locale); 
	/**
	 * SDE数据库的用户名
	 */
	private static final String USERNAME = "sde";
	
	/**
	 * SDE数据库的密码
	 */
	private static final String PASSWORD = "sde";
	
	/**
	 * SDE数据库的URL
	 */
	private static final String URL = "jdbc:oracle:thin:@192.168.15.208:1521:orcl";
	
	/**
	 * SDE数据库的数据源对象
	 */
	private static DataSource sdeDataSource;
	
	/**
	 * MIS数据库的用户名
	 */
	private static final String USERNAME_MIS = localResource.getString("USERNAME_MIS");
	
	/**
	 * MIS数据库的密码
	 */
	private static final String PASSWORD_MIS = localResource.getString("PASSWORD_MIS");
	
	/**
	 * MIS数据库的URL
	 */
	private static final String URL_MIS = localResource.getString("URL_MIS");
	
	/**
	 * MIS数据库的数据源对象
	 */
	private static DataSource misDataSource;
	
	
	/**
	 * MIS数据库的用户名
	 */
	private static final String USERNAME_SLGC = "gzwater";
	
	/**
	 * MIS数据库的密码
	 */
	private static final String PASSWORD_SLGC = "gzwater";
	
	/**
	 * MIS数据库的URL
	 */
	private static final String URL_SLGC = "jdbc:oracle:thin:@192.168.0.254:1521:orcl";
	
	/**
	 * MIS数据库的数据源对象
	 */
	private static DataSource SLGCDataSource;
	
	private ConnectionFactory() {
		
	}
	public DataSource getMisDataSource() {
		return misDataSource;
	}
	public void setMisDataSource(DataSource misDataSource) {
		ConnectionFactory.misDataSource = misDataSource;
	}
	
	public DataSource getSdeDataSource() {
		return sdeDataSource;
	}
	public void setSdeDataSource(DataSource sdeDataSource) {
		ConnectionFactory.sdeDataSource = sdeDataSource;
	}
	
	public static DataSource getSLGCDataSource() {
		return SLGCDataSource;
	}
	public static void setSLGCDataSource(DataSource SLGCDataSource) {
		ConnectionFactory.SLGCDataSource = SLGCDataSource;
	}
	/**
	 * 通过DriverManager获得SDE的数据连接
	 * @return
	 * @throws Exception
	 */
	public static Connection getSdeConnectionByDM() throws SQLException{
		Connection connetcion = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			throw new SQLException("找不到驱动程序");
		}
		
		connetcion = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		return connetcion;

	}	
	
	/**
	 * 通过DriverManager获得MIS的数据连接
	 * @return
	 * @throws Exception
	 */
	public static Connection getMisConnectionByDM() throws SQLException{
		Connection connetcion = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			throw new SQLException("找不到驱动程序");
		}
		System.out.println("get Mis Connection by DM");
		connetcion = DriverManager.getConnection(URL_MIS, USERNAME_MIS, PASSWORD_MIS);
		return connetcion;

	}	
	
	/**
	 * 通过DriverManager获得SLGC的数据连接
	 * @return
	 * @throws Exception
	 */
	public static Connection getSLGCConnectionByDM() throws SQLException{
		Connection connetcion = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			throw new SQLException("找不到驱动程序");
		}
		
		connetcion = DriverManager.getConnection(URL_SLGC, USERNAME_SLGC, PASSWORD_SLGC);
		return connetcion;

	}
	
	/**
	 * 获得SDE的数据库连接
	 * @return
	 * @throws SQLException
	 */
	public static Connection getSdeConnection() throws SQLException{
		if(sdeDataSource==null){
			System.out.println("Creating by DM");
			return getSdeConnectionByDM();
		}
		System.out.println("get from sdedatasource");
		return sdeDataSource.getConnection();
	}
	
	/**
	 * 获得MIS的数据库连接
	 * @return
	 * @throws SQLException
	 */
	public static Connection getMisConnection() throws SQLException{
		if(misDataSource==null){
			return getMisConnectionByDM();
		}
		return misDataSource.getConnection();
	}
	
	/**
	 * 获得MIS的数据库连接
	 * @return
	 * @throws SQLException
	 */
	public static Connection getSLGCConnection() throws SQLException{
		if(SLGCDataSource==null){
			return getSLGCConnectionByDM();
		}
		return SLGCDataSource.getConnection();
	}
	
}
